package com.openyelp.webstore.apps;

import java.io.File;
import java.io.IOException;

import org.apache.http.HttpEntity;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.mime.MultipartEntityBuilder;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;

public class HttpClientApps2 {

	public static void main(String[] args) throws ClientProtocolException,
			IOException {
		// TODO Auto-generated method stub
		CloseableHttpClient httpclient = HttpClients.createDefault();

		HttpPost httpPost = new HttpPost(
				"http://ad.xabaoyi.com:8080/webstore/image/upimgworks.do");

		File file = new File("D:\\uwx.jpg");
		MultipartEntityBuilder builder = MultipartEntityBuilder.create()
				.addBinaryBody("imgFile", file);
		builder.addTextBody("app", "ada");
		builder.addBinaryBody("imgFile", file);
		builder.addBinaryBody("imgFile", file);
		builder.addBinaryBody("imgFile", file);

		httpPost.setEntity(builder.build());
		CloseableHttpResponse response2 = httpclient.execute(httpPost);
		try {
			System.out.println(response2.getStatusLine());
			HttpEntity entity2 = response2.getEntity();
			// do something useful with the response body
			// and ensure it is fully consumed
			System.out.println(EntityUtils.toString(entity2));
			EntityUtils.consume(entity2);
		} finally {
			response2.close();
		}
	}

}
